package com.yxp423.ssmempl.controller;

import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxp423.ssmempl.bean.Leave;
import com.yxp423.ssmempl.bean.User;
import com.yxp423.ssmempl.service.ILeaveService;
import com.yxp423.ssmempl.service.ISessionService;
import com.yxp423.ssmempl.service.IUserService;
import com.yxp423.ssmempl.utils.base.AjaxResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description: java类作用描述
 * @Author: yxp
 * @CreateDate: 2019-05-06 11:30
 * @Version: 1.0
 */
@Controller
@RequestMapping("/leave")
public class LeaveController {

	@Autowired
	private ILeaveService leaveService;

	@Autowired
	IUserService userService;
	@Autowired
	ISessionService sessionService;

	Cache<String, IPage> leavePagesCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 5);//5分钟
	Cache<String, User> leaveUsersCache = CacheUtil.newLRUCache(10, DateUnit.MINUTE.getMillis() * 5);//5分钟

	@GetMapping("/applyleave")
	public String toApplyLeave() {


		return "leave/apply_leave";
	}


	@GetMapping("reviewleave")
	public String toReviewLeave() {
		return "leave/review_leave";
	}

	@GetMapping("myleave")
	public String toMyLevaePage() {

		return "leave/my_leave";
	}


	@GetMapping("/submitApply")
	@ResponseBody
	public AjaxResult submitApply(Leave leave) {
		AjaxResult result = new AjaxResult();

		result = leaveService.saveLeaveApply(leave);

		return result;

	}


	@GetMapping("myapplyleave")
	@ResponseBody
	public AjaxResult myApplyLeave(Page page) {

		AjaxResult result = new AjaxResult();
		User user = (User) SecurityUtils.getSubject().getPrincipal();
		if (ObjectUtil.isEmpty(user)) {

			result.setSuccess(false);
			result.setMsg("请登录");
			return result;
		}


		IPage<Leave> myLeave = leaveService.myLeave(user, page);


		if (ObjectUtil.isNotEmpty(myLeave.getRecords())) {

			for (Leave leaves : myLeave.getRecords()) {

				User user1 = userService.getUserById(Integer.parseInt(leaves.getReviewerId()));

				leaves.setUserName(user.getName());
				leaves.setReviewerName(user1.getName());

			}

			result.setSuccess(true);
			result.setData(myLeave);
		} else {
			result.setSuccess(false);
			result.setMsg("无数据");


		}

		return result;
	}


	@GetMapping("/getReviewLeave")
	@ResponseBody
	public AjaxResult getReviewLeave(Page page) {

		AjaxResult result = new AjaxResult();

//		long startTime = DateUtil.current(true);
		User user = (User) SecurityUtils.getSubject().getPrincipal();

		try {

			if (ObjectUtil.isNull(leavePagesCache.get("page" + page.getCurrent()))) {

				IPage<Leave> reviewLeave = leaveService.getReviewLeave(page, user);

				if (ObjectUtil.isNotNull(reviewLeave)) {

					for (Leave record : reviewLeave.getRecords()) {

						if (ObjectUtil.isNull(leaveUsersCache.get("user" + record.getUserId()))) {

							User userById = userService.getUserById(Integer.parseInt(record.getUserId()));

							record.setUserName(userById.getName());
							record.setReviewerName(user.getName());

							leaveUsersCache.put("user" + record.getUserId(), userById);

						} else {

							record.setUserName(leaveUsersCache.get("user" + record.getUserId()).getName());
							record.setReviewerName(user.getName());

						}

					}

					result.setSuccess(true);
					result.setMsg("获取数据成功");
					result.setData(reviewLeave);

					leavePagesCache.put("page" + page.getCurrent(), reviewLeave);

				} else {
					result.setSuccess(false);
					result.setMsg("获取分页数据失败");
				}
			} else {

				result.setSuccess(true);
				result.setData(leavePagesCache.get("page" + page.getCurrent()));

			}
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(e.toString());
		}

//		System.out.println("请假审核获取分页时间（分页"+page.getCurrent()+"）"+DateUtil.spendNt(startTime));
		return result;

	}

	@GetMapping("/submitOne")
	@ResponseBody
	public AjaxResult submitOne(Leave leave) {

		leavePagesCache.clear();
		return leaveService.updateLeave(leave);
	}

}
